/*
 * @Author: rxn 1003312535@qq.com
 * @Date: 2024-07-22 10:11:42
 * @LastEditors: rxn 1003312535@qq.com
 * @LastEditTime: 2024-08-07 13:50:35
 * @FilePath: \vue3-progect\src\views\example\three\js\init.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { CameraHelper } from "../case/helper/CameraHelper";
import { OrbitControlsHelper } from "../case/helper/OrbitControls";
//--------------------------------------------
import { WebGLRenderer } from "../create/renderer";
import { PerspectiveCamera } from "../create/camera";
/**
 * 创建相机实例
 * @returns
 */
export const createCamera = (THREE, container) => {
	const camera = PerspectiveCamera(
		THREE,
		90,
		container.offsetWidth / container.offsetHeight,
		0.1,
		1000
	);
	camera.position.z = 200;
	camera.position.y = 200;
	return camera;
};
/**
 * 动画执行函数
 * @param {*} renderer
 * @param {*} scene
 * @param {*} camera
 */
export const animate = (renderer, scene, camera) => {
	requestAnimationFrame(animate.bind(this, renderer, scene, camera));

	renderer.render(scene, camera);
};
/**
 * 创建渲染实例
 * @param {*} THREE
 * @param {*} container
 * @returns
 */
export const createRenderer = (THREE, container) => {
	const renderer = WebGLRenderer(THREE, container);
	return renderer;
};

export const initContainer = (THREE, container) => {
	//场景
	const scene = new THREE.Scene();
	const renderer = createRenderer(THREE, container);

	const camera = createCamera(THREE, container);
	CameraHelper(THREE, scene, camera);
	OrbitControlsHelper(THREE, scene, camera, renderer);
	animate(renderer, scene, camera);

	return { scene, renderer, camera };
};
